package org.boot.easyapi.permission.dao;

import org.boot.easyapi.permission.entity.PermissionDO;
import org.boot.easyapi.util.ObjectUtil;
import org.boot.easyapi.util.StrUtil;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Repository
public class PermissionRepository {

    @Resource
    private JdbcTemplate jdbcTemplate;

    @Transactional(rollbackFor = Exception.class)
    public int insert(PermissionDO p) {
        String sql = "INSERT INTO easyapi_core_permission(path,ip,type,status,default_path,note,path_role_json,path_menu_json) " +
                        "VALUES(?,?,?,?,?,?,?,?)";
        return jdbcTemplate.update(sql,
                p.getPath(), p.getIp(), p.getType(),
                p.getStatus(), p.getDefaultPath(),
                p.getNote(), p.getPathRoleJson(), p.getPathMenuJson());
    }


    @Transactional(rollbackFor = Exception.class)
    public int deleteById(Long id) {
        String sql = "DELETE FROM easyapi_core_permission WHERE id = ?";
        return jdbcTemplate.update(sql, id);
    }

    @Transactional(rollbackFor = Exception.class)
    public int updateAllById(PermissionDO p) {
        String sql = "UPDATE easyapi_core_permission SET path=?,ip=?,type=?,status=?,default_path=?,note=?,path_role_json=?,path_menu_json=? " +
                "WHERE id=?";
        return jdbcTemplate.update(sql,
                p.getPath(), p.getIp(), p.getType(),
                p.getStatus(), p.getDefaultPath(),
                p.getNote(), p.getPathRoleJson(), p.getPathMenuJson(),
                p.getId());
    }

    public PermissionDO selectById(Long id) {
        String sql = "SELECT * FROM easyapi_core_permission WHERE id = ?";
        List<PermissionDO> list = jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(PermissionDO.class), id);
        return list.isEmpty() ? null : list.get(0);
    }

    public List<PermissionDO> selectList(PermissionDO permission) {
        String sql = "SELECT * FROM easyapi_core_permission WHERE 1=1 " ;
        List<Object> args = new ArrayList<>();
        if (ObjectUtil.isNotEmpty(permission)){
            if (StrUtil.isNotEmpty(permission.getPath())){
                sql += " AND path = ?";
                args.add(permission.getPath());
            }
            if (StrUtil.isNotEmpty(permission.getIp())){
                sql += " AND ip = ?";
                args.add(permission.getIp());
            }
            if (ObjectUtil.isNotEmpty(permission.getStatus())){
                sql += " AND status = ?";
                args.add(permission.getStatus());
            }
            if (StrUtil.isNotEmpty(permission.getType())){
                sql += " AND type = ?";
                args.add(permission.getType());
            }
            if (ObjectUtil.isNotEmpty(permission.getDefaultPath())){
                sql += " AND default_path = ?";
                args.add(permission.getDefaultPath());
            }
        }
        return jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(PermissionDO.class), args.toArray());
    }
}
